$(function(){




    // 搜索栏
    $("#search").click(function(){

        let search=document.getElementById("search-input");
        search.style.background="rgba(0,10,32,0.09)";
    })

    $("#search").blur(function(){

        let search=document.getElementById("search-input");
        search.style.background="rgba(0,10,32,0.05)";
    })


// 标签栏搜索框
    $("#search1").click(function(){

        let search=document.getElementById("search-input-2");
        search.style.background="rgba(0,10,32,0.09)";
    })

    $("#search1").blur(function(){

        let search=document.getElementById("search-input-2");
        search.style.background="rgba(0,10,32,0.05)";
    })





// 轮播图控制

    $("#arrow_right").click(function(){
        $("#arrow_left").css("text-decoration","none");
        $(".Shuffling_figure_1").animate({
            left:'-885px'
        });
        $(".Shuffling_figure_2").animate({
            left:'-590px'
        });
        $(".Shuffling_figure_3").animate({
            left:'-295px'
        });
        $(".Shuffling_figure_4").animate({
            left:'0px'
        });
        $(".Shuffling_figure_5").animate({
            left:'295px'
        });

    })

// 轮播图控制
    $("#arrow_left").click(function(){

        $("#arrow_left").css("text-decoration","none");
        $(".Shuffling_figure_1").animate({
            left:'0px'
        });
        $(".Shuffling_figure_2").animate({
            left:'295px'
        });
        $(".Shuffling_figure_3").animate({
            left:'590px'
        });
        $(".Shuffling_figure_4").animate({
            left:'885px'
        });
        $(".Shuffling_figure_5").animate({
            left:'1180px'
        });

    })



// // 题单下拉动画
// $("#form-button-item1").click(function(){


//     if(form_button_item1==1){
//       $("#Suspension_1").focus();
//       $(".form-button").css("background-color","rgba(0,10,32,0.09)");
//       // 下拉样式动画
//       $("#Suspension_1").css("display","block");
//       $("#Suspension_1").css("transition","max-height 2s");
//       $("#Suspension_1").css("opacity","1");
//       $("#Suspension_1").css("max-height","500px");
//       $("#Suspension_1").css("height","auto");
//     }else{
//       $("#Suspension_1").css("border","none");
//       $(".form-button").css("background-color","rgba(0,10,32,0.05)");
//     // 下拉样式动画
//       $("#Suspension_1").css("opacity","0");
//       $("#Suspension_1").css("max-height","0px");
//       $("#Suspension_1").css("height","0");
//       $("#Suspension_1").css("transition","max-height 2s");  
//     }
//     form_button_item1=-form_button_item1;




// })

// $("#Suspension_1").blur(function(){


//     $("#Suspension_1").css("border","none");
//     $(".form-button").css("background-color","rgba(0,10,32,0.05)");
//     // 下拉样式动画
//     $("#Suspension_1").css("opacity","0");
//     $("#Suspension_1").css("max-height","0px");
//     $("#Suspension_1").css("height","0");
//     $("#Suspension_1").css("transition","max-height 2s");  
// })



// 分页栏
    $(".page_form").click(function(){


        if($("#page_choose").css("display")=="none"){
            $(".page_choose").focus().css("display","block");
            $("#page_choose").focus();
        }else{
            $(".page_choose").css("display","none");
        }
    })


    $(".page_choose").blur(function(){
        $(".page_choose").css("display","none");
    })

    $(".page_a").click(function(){
        let page_show=document.getElementById("page_show");
        page_show.innerText=this.innerText;

    })

});


function isLeapyear(year){
    return year%4==0 && year%100!=100 || year%400;
}

function getMaxDay(year,month){

    if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
        return 31;
    }else if(month==2){
        return isLeapyear(year)?29:28;
    }else{
        return 30;
    }
}
function get_week(year,month){
    var myDate = new Date(year,month-1,1);
    return myDate.getDay();

}


// 打印日历
window.onload=()=>{

    let str = "";//table tbody tr td ....  1到最大天数
    //获取年
    let year = new Date().getFullYear();
    let month = new Date().getMonth()+1;
    let nowYear = new Date().getFullYear();
    let nowmonth = new Date().getMonth()+1;
    let day=new Date().getDate();

    let hd_year=document.getElementById("year");
    hd_year.value=year;


    let hd_month=document.getElementById("month");
    hd_month.value=month;

    str+=`<table>
  <tr>
          <td>sun</td>
          <td>Mon</td>
          <td>Tue</td>
          <td>Wed</td>
          <td>Thu</td>
          <td>Fri</td>
          <td>Sat</td> 
  </tr>`;
    str+="<tr>";
    let count = 0;
    let daycount = 0;
    let week = get_week(year,month,1);



    for(let i=0;i<week;i++){
        str+="<td></td>";
        count++;
    }
    let max = getMaxDay(year,month);
    for(let i=1;i<=max;i++){
        daycount++;
        count++;
        if(day==daycount && month==nowmonth && year==nowYear){
            str+="<td style='background-color:rgba(0,122,255);border-radius: 50%;color:#fff'>"+i+"</td>";
        }else{
            str+="<td>"+i+"</td>";
        }

        if(count==7){
            count = 0;
            str+="</tr>";
            str+="<tr>";
        }
    }

    str+="</table>";

    let div = document.getElementById("content");
    div.innerHTML = str;

}




// 上个月日历      
$("#cal_left").click(function(){
    let str = "";//table tbody tr td ....  1到最大天数
    let year=document.getElementById("year").value;
    let month=document.getElementById("month").value;
    let nowYear = new Date().getFullYear();
    let nowmonth = new Date().getMonth()+1;
    let day=new Date().getDate();
    month=month-1;

    if(month==0){
        month=12;
        year=year-1;
    }
    let hd_year=document.getElementById("year");
    hd_year.value=year;

    let hd_month=document.getElementById("month");
    hd_month.value=month;
    str+=`<table>
<tr>
        <td>sun</td>
        <td>Mon</td>
        <td>Tue</td>
        <td>Wed</td>
        <td>Thu</td>
        <td>Fri</td>
        <td>Sat</td> 
</tr>`;
    str+="<tr>";
    let count = 0;
    let daycount = 0;
    let week = get_week(year,month,1);



    for(let i=0;i<week;i++){
        str+="<td></td>";
        count++;
    }
    let max = getMaxDay(year,month);
    for(let i=1;i<=max;i++){
        daycount++;
        count++;
        if(day==daycount && month==nowmonth && year==nowYear){
            str+="<td style='background-color:rgba(0,122,255);border-radius: 50%;color:#fff'>"+i+"</td>";
        }else{
            str+="<td>"+i+"</td>";
        }

        if(count==7){
            count = 0;
            str+="</tr>";
            str+="<tr>";
        }
    }

    str+="</table>";

    let div = document.getElementById("content");
    div.innerHTML = str;
    let monthshow =document.getElementById("cal_label");

    monthshow.children[1].innerText=month+"月";
})


// 下个月日历
$("#cal_right").click(function(){
    let str = "";//table tbody tr td ....  1到最大天数
    let year=document.getElementById("year").value;
    let month=document.getElementById("month").value;
    let nowYear = new Date().getFullYear();
    let nowmonth = new Date().getMonth()+1;
    let day=new Date().getDate();
    month=month*1+1;

    if(month==13){
        month=1;
        year=year*1+1;
    }
    console.log(month)
    let hd_year=document.getElementById("year");
    hd_year.value=year;

    let hd_month=document.getElementById("month");
    hd_month.value=month;
    str+=`<table>
<tr>
        <td>sun</td>
        <td>Mon</td>
        <td>Tue</td>
        <td>Wed</td>
        <td>Thu</td>
        <td>Fri</td>
        <td>Sat</td> 
</tr>`;
    str+="<tr>";
    let count = 0;
    let daycount = 0;
    let week = get_week(year,month,1);

    for(let i=0;i<week;i++){
        str+="<td></td>";
        count++;
    }
    let max = getMaxDay(year,month);
    for(let i=1;i<=max;i++){
        daycount++;
        count++;
        if(day==daycount && month==nowmonth && year==nowYear){
            str+="<td style='background-color:rgba(0,122,255);border-radius: 50%;color:#fff'>"+i+"</td>";
        }else{
            str+="<td>"+i+"</td>";
        }

        if(count==7){
            count = 0;
            str+="</tr>";
            str+="<tr>";
        }
    }

    str+="</table>";

    let div = document.getElementById("content");
    div.innerHTML="";
    div.innerHTML = str;
    let monthshow =document.getElementById("cal_label");

    monthshow.children[1].innerText=month+"月";
})




var level;
var state;
//分页
ajaxPage(1,20);

function layuiPage(count,curr,limit){
    layui.use(['laypage', 'layer'], function () {
        //分页
        var laypage = layui.laypage;
        laypage.render({
            elem: 'coding-page',
            layout: ['prev', 'page', 'next', 'skip'],
            count: count||0, //数据总数，从服务端得到
            limit: limit||2,
            curr: curr||1,
            jump: function (obj,first) {
                if (!first) {
                    let s=document.getElementsByName("checked");
                    ajaxPage(obj.curr,obj.limit);

                }
            }
        });
    })
}








function ajaxPage(page,size){
    //标签分类功能
    // 获取被选择的字段
    let s=document.getElementsByName("checked");
    let str="";
    for(let i=0;i<s.length;i++){
        str+=s[i].innerText+"、";
    }
    str=str.substring(-1,str.length-1);


    // 动态监听搜索框内的值
    let res = document.getElementById('search')
    res.oninput=function(){
        console.log(res.value);
        ajaxPage(1,20)
    }


//完成度状态显示

    $("#Suspension_3>#single").click(function(){
        $(".s3_state").css("display","none");
        $(this).children("img").eq(1).css("display","block");
         state=$(this).children("span").text();
        console.log(state);
        ajaxPage(page,size)
    });


    //难度选择
    $("#Suspension_2>#single").click(function(){
        $("#Suspension_2>#single>img").css("display","none");
        $(this).children("img").css("display","block");
        level=$(this).children("span").text();
        ajaxPage(page,size)
    });
    $.ajax({
        url: "/dev/page/all?page="+page+"&size="+size+"&label="+str+"&search="+res.value+"&level="+level,
        xhrFields: {withCredentials: true},	//前端适配：允许session跨域
        type: "get",
        dataType: "json",
        success: function (data) {
            console.log(level);
            console.log(data);
            if (data.code == 200) {
                let result = data.data.igoCodingEntities;
                let res = "";
                console.log(data);
                console.log(result);
                    let s=0;
                for (let i = 0; i < result.length; i++) {
                    res+="<div class='quetion_item1'>";
                    res+="<div class='quetion_label1'>";
                    res+="<img src='img/state/减号.png'>";
                    res+="</div>";
                    res+="<div class='quetion_label2'>";
                    if(result[i].codeContent.length>12){
                        let codeContent =result[i].codeContent.substring(-1,12)+"...";
                        res+="<span>"+result[i].id+"."+codeContent+"</span>";
                    }else{
                        res+="<span>"+result[i].id+"."+result[i].codeContent+"</span>";
                    }
                    res+="</div>";
                    res+="<div class='quetion_label3'>";
                    res+="<span>17028</span>";
                    res+="</div>";
                    res+="<div class='quetion_label4'>";
                    res+="<span>52.4%</span>";
                    res+="</div>";
                    res+="<div class='quetion_label5'>";
                    if(result[i].codeLevel=="Medium"){
                        res+="<span style='color: sandybrown'>中等</span>";
                    }else if(result[i].codeLevel=="Hard"){
                        res+="<span style='color: red'>困难</span>";
                    }else{
                        res+="<span style='color: seagreen'>简单</span>";
                    }
                    res+="</div>";
                    res+="<div class='quetion_label6'>";
                    res+="<span></span>";
                    res+="</div>";
                    res+="</div>";


                    if((++i)!=result.length){
                        res+="<div class='quetion_item2'>";
                        res+="<div class='quetion_label1'>";
                        res+="<img src='img/state/减号.png'>";
                        res+="</div>";
                        res+="<div class='quetion_label2'>";
                        if(result[i].codeContent.length>12){
                            let codeContent =result[i].codeContent.substring(-1,12)+"...";
                            res+="<span>"+result[i].id+"."+codeContent+"</span>";
                        }else{
                            res+="<span>"+result[i].id+"."+result[i].codeContent+"</span>";
                        }
                        res+="</div>";
                        res+="<div class='quetion_label3'>";
                        res+="<span>17028</span>";
                        res+="</div>";
                        res+="<div class='quetion_label4'>";
                        res+="<span>52.4%</span>";
                        res+="</div>";
                        res+="<div class='quetion_label5'>";
                        if(result[i].codeLevel=="Medium"){
                            res+="<span style='color: sandybrown'>中等</span>";
                        }else if(result[i].codeLevel=="Hard"){
                            res+="<span style='color: red'>困难</span>";
                        }else{
                            res+="<span style='color: seagreen'>简单</span>";
                        }
                        res+="</div>";
                        res+="<div class='quetion_label6'>";
                        res+="<span></span>";
                        res+="</div>";
                        res+="</div>";
                    }
                    $("#quetion").html(res);
                    layuiPage(data.data.total, data.data.current, data.data.size)
                }
            }
        });
}


// 分页选择栏
$("#page_20").click(function() {
    ajaxPage(1,20);
})
$("#page_50").click(function() {
        ajaxPage(1,50);
})
$("#page_100").click(function() {
        ajaxPage(1,100);
})


//按标签分页
$("#form-text>span").click(function() {

    if($(this).css("background-color")=="rgb(0, 122, 255)"){
        $(this).css("background-color","rgba(0, 10, 32, 0.05)");
        $(this).css("color","rgba(60,60,67,0.6)");
        $(this).attr("name","none");
    }else {
        $(this).css("background-color","#007AFF");
        $(this).css("color","#FFFFFF");
        $(this).attr("name","checked");

    }
    ajaxPage(1,20);
});



